﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QuanLyTiemVang.DTO;

namespace QuanLyTiemVang.DAO
{
    class PhieuChiDAO
    {
        private static QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();

        public static List<PHIEUCHI> GetAllPhieuChi()
        {
            return db.PHIEUCHIs.ToList();
        }
        public static List<PHIEUCHI> SelectPhieuChi()
        {
            return db.PHIEUCHIs.ToList();
        }
        public static List<PHIEUCHI> SelectPhieuChiByNgayChi(PHIEUCHI pc)
        {
            var query = from d in db.PHIEUCHIs
                        where d.NgayChi == pc.NgayChi
                        select d;

            return query.ToList();
        }    
        public static PHIEUCHI GetPhieuChi(DateTime date)
        {
            var query = from d in db.PHIEUCHIs
                    where d.NgayChi == date
                    select d;
            
            return query.First();
        }
        public static int Insert(PHIEUCHI pc)
        {
            db.PHIEUCHIs.Add(pc);
            db.SaveChanges();
            return pc.MaPhieuChi;
        }
        public static void Delete(PHIEUCHI pc)
        {
            var query = db.PHIEUCHIs.Find(pc.MaPhieuChi);
            if (query != null)
            {
                db.PHIEUCHIs.Remove(pc);
                db.SaveChanges();
            }
        }

        public static void Update(PHIEUCHI pc)
        {
            var query = db.PHIEUCHIs.Find(pc.MaPhieuChi);
            if(query != null)
            {
                query.MaDanhSachKhoanChi = pc.MaDanhSachKhoanChi;
                query.NgayChi = pc.NgayChi;
                query.TongCong = pc.TongCong;
                db.SaveChanges();   
            }
        }
        // news Tam
        public static PHIEUCHI getPhieuChi(DateTime date)
        {
            var result =( from pc in db.PHIEUCHIs
                         where pc.NgayChi == date
                         select pc).ToList();
            if (result.Count() > 0)
                return result.First();
            return null;
        }
        public static List<PHIEUCHI> getListPhieuChi(DateTime date)
        {
            var result = (from pc in db.PHIEUCHIs
                          where pc.NgayChi == date
                          select pc).ToList();
            if (result.Count() > 0)
                return result;
            return null;
        }

        public static List<CHITIETPHIEUCHI> getAllChiTietPhieuChi(PHIEUCHI pc)
        {
            var query = from tcpc in db.CHITIETPHIEUCHIs
                        where tcpc.MaPhieuChi == pc.MaPhieuChi
                        select tcpc;
            if (query.Count() > 0)
            {
                return query.ToList();
            }
            return null;
        }

        public static List<KHOANPHI> GetAllKhoanPhi()
        {
            return db.KHOANPHIs.ToList();
        }
    }
}
